#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"



/********************************************************************************************
********************************************************************************************
    UD61720  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    UD61720  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    UD61720  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    UD61720  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    UD61720  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void UD61720_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	  u8 buffer1[7];
		if(USB_Rx_Buffer[1]&0x80)
			SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		else
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);	
    SSD2828_W_Array(SigMode,channel,buffer,2);  
    delay_ms(5);		

    buffer1[0] = 0x02;        
    buffer1[1] = 0x80;
    buffer1[2] = 0x10;    
    SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	  delay_ms(5); 		

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void UD61720_Write_51Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
	  //SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(5);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void UD61720_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
		buffer[0]=0x4C;	
		buffer[1]=0x02;
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}

void UD61720_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
//    u8 i,j,m,k=0,temp;
	
	  SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
		delay_ms(5);
	
	  buffer1[0] = 0x02;        
    buffer1[1] = 0x80;
    buffer1[2] = 0x10;    
    SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	  delay_ms(5); 
	
	  if((buffer[3]&0xFF)==0x00)       //GAMMA RED
	  {
				 SSD2828_W_Reg(SigMode,channel,0xBC,22); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xC5);    //GAMMA RED   
	  }
		else if((buffer[3]&0xFF)==0x42)  //GAMMA GREEN
		{	
				 SSD2828_W_Reg(SigMode,channel,0xBC,22); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xC6); //GAMMA GREEN 
		}
		else if((buffer[3]&0xFF)==0x81)   //GAMMA BLUE
		{		
				 SSD2828_W_Reg(SigMode,channel,0xBC,22); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xC7); //GAMMA BLUE 
		} 
    	
		   for( i = 0;i<21;i++)   //21个绑点
		   {	                                                                                  
			   SSD2828_W_Data(SigMode,channel,buffer[i+4]);	
			   delay_ms(5);  
		   }
		
		buffer1[0] = 0x02;        
    buffer1[1] = 0xBE;
    buffer1[2] = 0x01;    
    SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	  delay_ms(5); 
				
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //返回 写寄存器状态  ：ok  
	  SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void UD61720_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u16 tmp;
    u8 i;
		
		 if((buffer[3]&0x0F)==0x00)       //GAMMA RED
	  {   
				 SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x0016); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xC5;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1
		  
	  }
		else if((buffer[3]&0x0F)==0x04)  //GAMMA GREEN
		{
			  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x0016); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xC6;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1   
		}
		else if((buffer[3]&0x0F)==0x08)   //GAMMA BLUE
		{
			  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x0016); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xC7;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1 
		   
		} 
		
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		delay_ms(5);
		
			    for(i=0;i<21;i++)
		  {
				  SSD2828_W_Cmd(SigMode,channel,0xFA);		
				  tmp=SPI3_Read_u16_Data(channel);
				  buffer[4+i]=tmp>>8;
				  buffer[5+i]=tmp;
			    delay_ms(5);
				  i++;			
		  }
			
    buffer[0]=0x4C;	//UD61720
    buffer[1]=0x09;
		delay_ms(15);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //返回 读取的 Gamma数据  ：ok  		

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void UD61720_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[10];
//	  u8 mtp_flag;
	  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15); 
	
	  buffer1[0] = 0x02;        
    buffer1[1] = 0x80;
    buffer1[2] = 0x10;    
    SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	  delay_ms(5); 				
	  
//	  buffer1[0] = 0x02;        
//    buffer1[1] = 0x10;
//    buffer1[2] = 0x00;    
//    SSD2828_W_Array(SigMode,channel,buffer1,0);		 
//	  delay_ms(100); 	

//    buffer1[0] = 0x02;        
//    buffer1[1] = 0x80;
//    buffer1[2] = 0x10;    
//    SSD2828_W_Array(SigMode,channel,buffer1,0);		 
//	  delay_ms(5); 		
	
	 if(buffer[1]==0x0A)  //otp Gamma
	 {
		 buffer1[0] = 0x06;
     buffer1[1] = 0x82;
		 buffer1[2] = 0x00;
     buffer1[3] = 0x00; 
     buffer1[4] = 0x00;
		 buffer1[5] = 0x04;
     buffer1[6] = 0x01;		
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(500); 
	 }
	 else if(buffer[1]==0x0D)  //otp GOA
	 {
		 buffer1[0] = 0x06;
     buffer1[1] = 0x82;
		 buffer1[2] = 0x00;
     buffer1[3] = 0x00; 
     buffer1[4] = 0x00;
		 buffer1[5] = 0x05;
     buffer1[6] = 0x01;	  
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(500); 
	 }
    else if(buffer[1]==0x0E)  //otp POWER
	 {
		 buffer1[0] = 0x06;
     buffer1[1] = 0x82;
		 buffer1[2] = 0x00;
     buffer1[3] = 0x00; 
     buffer1[4] = 0x00;
		 buffer1[5] = 0x06;
     buffer1[6] = 0x01; 
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(500); 
	 }
   else if(buffer[1]==0x0F)  //otp OTHER
	 {
		 buffer1[0] = 0x06;
     buffer1[1] = 0x82;
		 buffer1[2] = 0x00;
     buffer1[3] = 0x00; 
     buffer1[4] = 0x00;
		 buffer1[5] = 0x07;
     buffer1[6] = 0x01; 
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(500); 
	 }
		
		buffer[1] = 0x0A;
		buffer[2] = 0x02;
		buffer[3] = Uart_Error_None;   //返回 OTP Gamma数据  ：OK
		buffer[4] = 0;
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);         
}


void UD61720_Read_OTP_Times(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
}

void ProcessForIc4C( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1]&0x0F)
	{
			case 0x01:                                      //写寄存器
				UD61720_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x02:                                      //读寄存器
				UD61720_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x03:                                      //写寄存器
				UD61720_Write_51Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;															
			case 0x08:                                      //写Gamma数据    
				UD61720_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x09:                                      //读Gamma数据
				UD61720_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break; 
			case 0x0A://gamma otp
										case 0x0D://OTP GOA
			case 0x0E://OTP POWER
			case 0x0F://OTP OTHERS	
				UD61720_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;
			case 0x0B://OTP times
				UD61720_Read_OTP_Times(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;							
			default:	break;	
	}			
}


